//
//  SettingViewController.swift
//  AIPhotoProcess
//
//  Created by aw031 on 3/12/2025.
//

import UIKit
import SnapKit

class SettingViewController: BaseViewController , UITableViewDelegate, UITableViewDataSource  {
    
    
    lazy var tableView: UITableView = {
        
        let tbView = UITableView.init(frame: .zero, style: .grouped)
        tbView.delegate = self
        tbView.dataSource = self
        tbView.backgroundColor = .clear
        tbView.separatorStyle = .none
        tbView.contentInsetAdjustmentBehavior = .never;
        tbView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0);
        //        tbView.tableHeaderView = createHeaderView()
        tbView.tableHeaderView = UIView.creatHeaderAndFooterView();
        tbView.tableFooterView = UIView.creatHeaderAndFooterView();
        tbView.showsVerticalScrollIndicator = false;
        tbView.showsVerticalScrollIndicator = false;
        tbView.register(SettingHomeCell.self, forCellReuseIdentifier: SettingHomeCell.identifier)
        
        return tbView
    }()
    
    private let rows = [
        (title : "Rate us on the App Store", iconName:"setRateIcon",tag:0),
        (title : "Share the app with friends", iconName:"setShareIcon",tag:1),
        (title : "Send us feedback", iconName:"setSendIcon",tag:2),
        (title : "User terms", iconName:"setUserTermsIcon",tag:3),
        (title : "Privacy agreement",iconName: "setPrivacyIcon",tag:4)
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addTopImageView()
        addNavigationBar(.leftAndTitle) { [weak self] (btn,index)  in
            self?.navigationController?.popViewController(animated: true)

        }
        
        navigationViewBar.centerBtn.isHidden = true
        setUpdataUI()
    }
    
    
    // MARK: - TableView
    private func setUpdataUI() {
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints({ make in
            make.top.equalTo(KStatusAndNavigationHeight)
            make.leading.trailing.bottom.equalToSuperview()
        })
    }
    
    // MARK: - TableView DataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rows.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return KScreenWValue(60)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SettingHomeCell.identifier, for: indexPath) as! SettingHomeCell
        let tup = rows[indexPath.row]
        cell.configure(title: tup.title, iconName: tup.iconName , isHiddenBottomLineView: (rows.count == indexPath.row + 1))
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let tup = rows[indexPath.row]
        
        switch tup.tag {
        case 0:
            //评分
            ScoreAlertTools.shared.showAppReview()
        case 1:
            //分享app
            Utility.shareApp(target: self)
        case 2:
            //发送
            self.pushSettingSendFeedbackViewController()
        case 3:
            //user of tmers
            Utility.openExternalLink(urlString: KUserOfTermsURL)
        case 4:
            //privacy
            Utility.openExternalLink(urlString: KPrivacyPolicyURL)
        default:
            return
        }
        
        
    }
}
